<?php
namespace app\app\controller\v1;
use app\app\model\CardModel;
use app\app\util\OnlineUtils;

class CardController extends BaseController{

    /**
     * 上传音频
     */
    public function saveAudio()
    {
        $user_id = OnlineUtils::getUserIdBySid();

        $data['address'] = input('post.address') ? input('post.address') : '';   //地址
        $data['phone'] = input('post.phone') ? input('post.phone') : '';       //手机号
        $data['duties'] = input('post.duties') ? input('post.duties') : '';    //职务
        $data['wechat_num'] = input('post.wechat_num') ? input('post.wechat_num') : '';   //微信号
        $data['corporare_name'] = input('post.corporare_name') ? input('post.corporare_name') : '';  //公司名称
        $data['content'] = input('post.content') ? input('post.content') : '';    //个人简介
        $id = input('post.id') ? input('post.id') : 0;
        
        if(!$id){
            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file('file');

            // 移动到框架应用根目录/public/uploads/ 目录下
            try {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/audio');
            } catch (Exception $e) {
                return jsonReturn(402, $this->error());
            }
            
            if ($info) {
                // 成功上传后 获取上传信息
                $audio = $info->getSaveName();
                $card = new CardModel();
                try {
                    $audio = $card->saveData($user_id, $audio, $data);
                } catch (Exception $e) {
                    return jsonReturn(402, $this->error());
                }
            } else {
                return jsonReturn(402, $file->getError());
            }
            return jsonReturn(200, 'success', $audio);
        }else{
            //修改数据
            $card = new CardModel();
            try{
                $audio = $card->saveCardByCardId($id,$data);
            }catch(\Exception $e){
                return jsonReturn(402,$e->getMessage());
            }
            return jsonReturn(200,'success',$audio);
        }
    }

}